<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\OSS;

class UploadController extends Controller
{
    //
    public function upload(Request $request){
        $pic = $request->file('pic');
        // 判断图片有效性
      //  dd($pic->getClientMimeType());
        if (!$pic->isValid()) {
        return back()->withErrors('上传图片无效..');
        }
        // 获取图片在临时文件中的地址
        $realPath = $pic->getRealPath();
        // 制作文件名
        $key = time() . rand(10000, 99999999) . '.jpg';
        //阿里 OSS 图片上传
        $result=OSS::upload('upload/'.$key, $realPath,['ContentType'=>$pic->getClientMimeType()]);
       // $result = OSS::upload($key, $pic);
        if ($result) {
            //dd($result);
            dd(OSS::getUrl('upload/'.$key));
        // success
        } else {
        // fail
        }
    }
}
